package com.ludomancy.gamelib;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;


public class MessageBox extends DialogWidget implements Button.ButtonListener
{
	private Button okButton;
	private MessageBoxListener listener;
	private TextWidget text;
	
	public interface MessageBoxListener
	{
		void onMessageBoxClosed(MessageBox mb);
	}
	
	public MessageBox(Widget parent, MessageBoxListener listener, String message, String buttonLabel)
	{
		super(parent, new Rectangle());
		// center the box
		int width = (parent.getWidth() * 2)/3;
		int fontHeight = Utils.stringSize(GameCanvas.screen.theme.getDefaultFont(), message).y; 
		int height = fontHeight * 10;
		Point center = Utils.getCenterInRect(new Point(width,height), parent.getRelativeRect());
		construct(listener, new Rectangle(center.x, center.y, width, height), message, buttonLabel);
	}
	
	public MessageBox(Widget parent, MessageBoxListener listener, Rectangle area, String message, String buttonLabel)
	{
		super(parent, area);
		construct(listener, area, message, buttonLabel);
	}
	
	private void construct(MessageBoxListener listener, Rectangle area, String message, String buttonLabel)
	{
		this.listener = listener;
		setRelativeRect(area);
		// create button
		Point stringSize = Utils.stringSize(GameCanvas.screen.theme.getDefaultFont(), buttonLabel);
		int buttonWidth = stringSize.x*2;
		int buttonHeight = stringSize.y*2;
		Point center = Utils.getCenterInRect(new Point(buttonWidth,buttonHeight), new Rectangle(0,0,area.width,area.height));
		okButton = new Button(this, new Rectangle(center.x,area.height - buttonHeight - 10,buttonWidth,buttonHeight), buttonLabel, this);
		// create text
		text = new TextWidget(this, new Rectangle(3,3,getWidth()-6, getHeight()-6-okButton.getHeight()));
		text.setText(message);
	}
	
	public void paint(Graphics g)
    {
    	// paint background
		GameCanvas.screen.theme.fillUIBackground(g, new Rectangle(0,0,getWidth(), getHeight()));
        g.setFont(GameCanvas.screen.theme.getDefaultFont());
        g.setColor(GameCanvas.screen.theme.getDefaultTextColor());
    }

	public void onPress(Button widget)
	{
		if(widget == okButton)
		{
			listener.onMessageBoxClosed(this);
			close();
		}
	}	
}
